package ar.edu.unq.wannaplay.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

import ar.edu.unq.wannaplay.exceptions.NegativeNumberException;
import ar.edu.unq.wannaplay.pages.employee.EditEmployeeLicencesPage;
import ar.edu.unq.wannaplay.services.EmployeeService;
import ar.edu.unq.wannaplay.utils.EmployeeEditer;

public class AddLicenceTypePanel extends ServicedPanel<EmployeeService> {

    private static final long serialVersionUID = 1L;

    private final EmployeeEditer employeeEditer;

    private String licenceName = "";

    private int maxPerYear = 0;

    private int maxConsecutive = 0;

    public AddLicenceTypePanel(final String id, final EmployeeEditer employeeEditer, final EmployeeService service) {
        super(id, service);
        this.employeeEditer = employeeEditer;
        this.initialize();
    }

    private void initialize() {
        Form form = new Form("form", new CompoundPropertyModel(this));
        form.add(new TextField("licenceName"));
        form.add(new TextField("maxPerYear"));
        form.add(new TextField("maxConsecutive"));
        form.add(this.createAjaxButon("accept"));
        this.add(form);
    }

    public void acceptAction() throws NegativeNumberException {
        this.getService().getLicenceService().addLicenceType(licenceName, maxPerYear, maxConsecutive);
    }

    private AjaxButton createAjaxButon(final String id) {
        return new AjaxButton(id) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                try {
                    AddLicenceTypePanel.this.acceptAction();
                    this.setResponsePage(new EditEmployeeLicencesPage(AddLicenceTypePanel.this.getEmployeeEditer(),
                            AddLicenceTypePanel.this.getService()));
                } catch (NegativeNumberException e) {
                    // reportar error?
                }
            }
        };
    }

    public EmployeeEditer getEmployeeEditer() {
        return employeeEditer;
    }

    public void setMaxConsecutive(final int maxConsecutive) {
        this.maxConsecutive = maxConsecutive;
    }

    public int getMaxConsecutive() {
        return maxConsecutive;
    }

    public void setMaxPerYear(final int maxPerYear) {
        this.maxPerYear = maxPerYear;
    }

    public int getMaxPerYear() {
        return maxPerYear;
    }

    public void setLicenceName(final String licenceName) {
        this.licenceName = licenceName;
    }

    public String getLicenceName() {
        return licenceName;
    }

}
